/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.dao.impl;

import cz.muni.fi.pa165.creatures.dao.CreatureTypeWeaponDAO;
import cz.muni.fi.pa165.creatures.model.CreatureType;
import cz.muni.fi.pa165.creatures.model.CreatureTypeWeapon;
import cz.muni.fi.pa165.creatures.model.Weapon;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * This class represents the implementation of all basic operations upon
 * {@link cz.muni.fi.pa165.creatures.model.CreatureTypeWeapon} instances.
 *
 * @author smikloso
 */
@Repository
@Transactional
public class CreatureTypeWeaponDAOImpl implements CreatureTypeWeaponDAO {

    @PersistenceContext
    private EntityManager em;
    
    private static final Logger logger =
            Logger.getLogger(CreatureDAOImpl.class.getName());

    @Override
    public void create(CreatureTypeWeapon creatureTypeWeapon) {
        try {
            DAOUtils.notNull(creatureTypeWeapon);
            em.persist(creatureTypeWeapon);
        } catch (IllegalArgumentException ex) {
            logger.log(Level.INFO, "Unable to create a creatureTypeWeapon. "
                    + "It cannot be a null object.");
        } catch (EntityExistsException ex) {
            logger.log(Level.INFO, "Unable to create a creatureTypeWeapon. "
                    + "It is already created.");
        }
    }

    @Override
    public void remove(CreatureTypeWeapon creatureTypeWeapon) {
        try {
            DAOUtils.notNull(creatureTypeWeapon);
            em.remove(creatureTypeWeapon);
        } catch(IllegalArgumentException ex) {
            logger.log(Level.INFO, "Unable to remove a creatureTypeWeapon. "
                    + "It cannot be a null object.");
        }
    }

    @Override
    public void update(CreatureTypeWeapon creatureTypeWeapon) {
        try {
            DAOUtils.notNull(creatureTypeWeapon);
            DAOUtils.notNull((creatureTypeWeapon.getCreatureType()));
            DAOUtils.notNull(creatureTypeWeapon.getWeapon());
            DAOUtils.notNull(creatureTypeWeapon.getEffectivity());
            em.merge(creatureTypeWeapon);
        } catch(IllegalArgumentException ex) {
            logger.log(Level.INFO, "Unable to update creatureTypeWeapon, "
                    + "at least one of its attributes is null, which is not permitted.");
        }
    }

    @Override
    public CreatureTypeWeapon get(CreatureType creatureType, Weapon weapon) {
        try {
            DAOUtils.notNull(creatureType);
            DAOUtils.notNull(weapon);
            
            TypedQuery<CreatureTypeWeapon> query = 
                    em.createQuery(
                        "select ctw from CreatureTypeWeapon ctw where "
                        + "creatureType = :creatureType "
                        + "and "
                        + "weapon = :weapon", 
                        CreatureTypeWeapon.class);
            
            query.setParameter("creatureType", creatureType);
            query.setParameter("weapon", weapon);
            try {
                return query.getSingleResult();
            } catch(NoResultException ex) {
                // if no record is returned, this exception is thrown,
                // which indicates we return null
                return null;
            }
        } catch(IllegalArgumentException ex) {
            logger.log(Level.INFO, "Unable to get a creatureTypeWeapon, one of "
                    + "creatureType or weapon (or both) are null objects");
            return null;
        }
    }

    @Override
    public int getCount() {
        Query q = em.createQuery("select count(o) from CreatureTypeWeapon as o");
        return ((Long) q.getSingleResult()).intValue();        
    }

    @Override
    public List<CreatureTypeWeapon> getAll() {
        Query q = em.createQuery("select cwt from CreatureTypeWeapon cwt "
                + "order by cwt.creatureType, cwt.weapon");
        return q.getResultList();
    }
}
